package com.innovation.chengzhangriji.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;

import com.innovation.chengzhangriji.Baby;
import com.innovation.chengzhangriji.BabyAndParent;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.database.DatabaseManager;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.innovation.chengzhangriji.ui.widget.CuteImageView;
import com.innovation.chengzhangriji.ui.widget.CuteTextView;
import com.innovation.chengzhangriji.ui.widget.CuteToolbar;
import com.innovation.chengzhangriji.ui.widget.CuteTwoButton;

import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;

public class BabyActivity extends BaseActivity implements CuteToolbar.OnClickCuteToolbarListener, View.OnClickListener {

    private int mState = -1;

    private Baby mBaby;

    private String mUrl;

    private String mName;

    private String mDay;

    private String mOldRole;

    private BabyAndParent babyAndParent;

    private CuteToolbar mCuteToolbar;
    private CuteTwoButton mButtonBaby;
    private CuteTwoButton mButtonAll;
    private CuteTwoButton mButtonPhoto;
    private CuteTwoButton mButtonVideo;
    private CuteTextView mTextQRCode;

    // Fragment
    private BabyInfoFragment mBabyInfoFragment;
    private BabyAllFragment mBabyAllFragment;
//    private HistoryFragment mHistoryFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        queryBabyInfo();
        if(mBaby == null) {
            finish();
            return;
        }
        setupLayout();
    }

    @Override
    public void onClickLeft() {

        switch (mState) {
            case 0:
                mBabyInfoFragment.onClickLeft();
                break;
            case 1:
                finish();
                break;
            case 2:
                finish();
                break;
            case 3:
                finish();
                break;
        }
    }

    @Override
    public void onClickRight() {
        if (babyAndParent.getRole() == 0) { // 成员
            Intent intent = new Intent(BabyActivity.this, NormalInviteActivity.class);
            startActivity(intent);
        } else { // 管理员,超级管理员
            Intent intent = new Intent(BabyActivity.this, AdminInviteActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_baby:
                switchState(0);
                break;
            case R.id.button_all:
                switchState(1);
                break;
            case R.id.button_photo:
                switchState(2);
                break;
            case R.id.button_video:
                switchState(3);
                break;
            case R.id.image_qrcode:
                openQRDialog();
                break;
        }
    }

    public CuteToolbar getCuteToolbar() {
        return mCuteToolbar;
    }

    private void setupLayout() {
        setContentView(R.layout.activity_baby);
        mCuteToolbar = (CuteToolbar) findViewById(R.id.toolbar);
        mCuteToolbar.setOnClickCuteToolbarListener(this);
        mCuteToolbar.setCuteTitle(mBaby.getName() + "的成长日记");
        mButtonBaby = (CuteTwoButton) findViewById(R.id.button_baby);
        mButtonAll = (CuteTwoButton) findViewById(R.id.button_all);
        mButtonPhoto = (CuteTwoButton) findViewById(R.id.button_photo);
        mButtonVideo = (CuteTwoButton) findViewById(R.id.button_video);
        mTextQRCode = (CuteTextView) findViewById(R.id.text_note_name);
        mTextQRCode.setText(mBaby.getName() + "的成长日记");
        CuteImageView imageQRCode = (CuteImageView) findViewById(R.id.image_qrcode);

        mButtonBaby.setOnClickListener(this);
        mButtonAll.setOnClickListener(this);
        mButtonPhoto.setOnClickListener(this);
        mButtonVideo.setOnClickListener(this);
        imageQRCode.setOnClickListener(this);

        mBabyInfoFragment = new BabyInfoFragment();
        mBabyAllFragment = new BabyAllFragment();
//        mHistoryFragment = new HistoryFragment();

        switchState(0);
    }

    private void switchState(int state) {
        // TODO 临时代码
        if (state == 3) {
            tipShow("敬请期待");
            return;
        }

        if (mState == state) {
            return;
        }

        mState = state;
        mButtonBaby.setOneState();
        mButtonAll.setOneState();
        mButtonPhoto.setOneState();
        mButtonVideo.setOneState();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        switch (state) {
            case 0:
                mButtonBaby.setTwoState();
                ft.replace(R.id.content, mBabyInfoFragment);
                ft.commit();
                break;
            case 1:
                mButtonAll.setTwoState();
                ft.replace(R.id.content, mBabyAllFragment);
                ft.commit();
                break;
            case 2:
//                mButtonPhoto.setTwoState();
//                ft.replace(R.id.content, mHistoryFragment);
//                ft.commit();
                break;
            case 3:
                mButtonVideo.setTwoState();
                break;
        }
    }

    public Baby getBaby() {
        return mBaby;
    }

    public void updateBaby(Baby baby) {
        DatabaseManager.getInstance(this).updateBaby(baby);
    }

    public void updateTimeline(int babyID, String newRole) {
        if (mOldRole.equals(newRole)) {
            return;
        }
        DatabaseManager.getInstance(this).updateRole(babyID, mOldRole, newRole);
    }

    private void queryBabyInfo() {
        LoginInfo login = Config.getLoginInfo(this);
        mBaby = DatabaseManager.getInstance(this).queryBaby(login.getUserID(), getIntent().getIntExtra("babyId", 0));
        if(mBaby == null) {
            tipShow("大家长已取消您对该宝宝的关注!");
            return;
        }
        mOldRole = mBaby.getParentName();
        mUrl = mBaby.getImageUrl();
        mName = mBaby.getName();
        switch (mBaby.getState()) {
            case Client.STATE_READY:
                mDay = getBabyDay(mBaby);
                break;
            case Client.STATE_START:
                mDay = getBabyDay(mBaby);
                break;
            case Client.STATE_END:
                mDay = getBabyDay(mBaby);
                break;
        }
        babyAndParent = DatabaseManager.getInstance(this).queryBabyAndParent(login.getUserID(), login.getBabyID());
    }

    private void openQRDialog() {
        Intent intent = new Intent(this, BabyQRCodeDialogActivity.class);
        intent.putExtra("url", mUrl);
        intent.putExtra("name", mName);
        intent.putExtra("day", mDay);
        startActivity(intent);
    }

    private String getBabyDayByDueDateOrLastDate(long time) {
        DateTime startTime = new DateTime(time);
        DateTime endTime = new DateTime(System.currentTimeMillis());
        Period pWeek = new Period(startTime, endTime, PeriodType.weeks());
        String week = "";
        String day = "";
        if (pWeek.getWeeks() != 0) {
            week = pWeek.getWeeks() + "周";
        }
        Period pDay = new Period(startTime, endTime, PeriodType.yearWeekDay());
        if (pDay.getDays() != 0) {
            day = pDay.getDays() + "天";
        }
        if (TextUtils.isEmpty(week) && TextUtils.isEmpty(day)) {
            return "孕1天";
        }
        return "孕" + week + day;
    }

    private String getBabyDayByBirthday(long birthday) {
        DateTime startTime = new DateTime(birthday);
        DateTime endTime = new DateTime(System.currentTimeMillis());
        Period p = new Period(startTime, endTime, PeriodType.yearMonthDay());
        String year = "";
        String month = "";
        String day = "";
        if (p.getYears() != 0) {
            year = p.getYears() + "岁";
        }
        if (p.getMonths() != 0) {
            month = p.getMonths() + "月";
        }
        if (p.getDays() != 0) {
            day = p.getDays() + "天";
        }
        return year + month + day;
    }

    private String getBabyDay(Baby baby) {
        switch (baby.getState()) {
            case Client.STATE_READY:
                return "备孕中";
            case Client.STATE_START:
                if (baby.getDueDate() != 0) {
                    return getBabyDayByDueDateOrLastDate(baby.getDueDate() - 280 * 24 * 60 * 60 * 1000l);
                } else {
                    return getBabyDayByDueDateOrLastDate(baby.getLastDate());
                }
            case Client.STATE_END:
                return getBabyDayByBirthday(baby.getBirthday());
        }
        return null;
    }


}
